package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;
import com.tangkun.environment.view.PaintView;
import com.tangkun.environment.view.SquareLayout;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 签字
 */
public class QianZiActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.fl_qianzi)
    FrameLayout mFrameLayout;
    @BindView(R.id.squarelayout)
    SquareLayout squarelayout;
    private Context mContext;

    /**
     * 画笔工具
     */
    private PaintView mPaintView;
    /**
     * 签名返回的bitmap
     */
    private Bitmap mSignBitmap;
    /**
     * 签名文件本地存储地址
     */
    private String path = "";
    /**
     * 签名文件
     */
    private File file = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qianzi);
        mContext = this;
        ButterKnife.bind(this);
        title.setText("签字");
        next.setText("确定");

        // 获取屏幕尺寸
        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
        int screenWidth = mDisplayMetrics.widthPixels;
        int screenHeight = mDisplayMetrics.heightPixels;
        mPaintView = new PaintView(mContext, screenWidth, screenHeight);
        mFrameLayout.addView(mPaintView);
        mPaintView.requestFocus();
    }

    @OnClick({R.id.back, R.id.next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                Utils.hideSoftKeyboard(QianZiActivity.this, back);
                JumpHelper.finish(mContext);
                break;
            case R.id.next:
                if (mPaintView.getPath().isEmpty()) {
                    showToast("请签名");
                    return;
                }
                mSignBitmap = (Bitmap) mPaintView.getPaintBitmap();
                createSignFile();
                Intent intent = new Intent();
                //拼接成文件集合形式
                List<File> fileList = new ArrayList<File>();
                fileList.add(file);//签名文件存储地址
                intent.putExtra("fileList", (Serializable) fileList);
                setResult(RESULT_OK, intent);
                Utils.hideSoftKeyboard(QianZiActivity.this, back);
                JumpHelper.finish(mContext);
                break;
        }
    }

    /**
     * 创建签名文件
     */
    private void createSignFile() {
        ByteArrayOutputStream baos = null;
        FileOutputStream fos = null;
        try {
//            path = GlobalUrl.CACHE_PATH + System.currentTimeMillis() + ".jpg";
            path = GlobalUrl.CACHE_PATH + System.currentTimeMillis();
            file = new File(path);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            file.createNewFile();
            fos = new FileOutputStream(file);
            baos = new ByteArrayOutputStream();
            //如果设置成Bitmap.compress(CompressFormat.JPEG, 100, fos) 图片的背景都是黑色的
            mSignBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();
            if (b != null) {
                fos.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
